package cn.ycc1.order.controller;

import cn.ycc1.core.api.ApiResult;
import cn.ycc1.order.entity.Order;
import cn.ycc1.order.service.OrderService;
import cn.ycc1.order.vo.OrderVo;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Title:
 * @Author ycc
 * @Date 2023/7/4 16:22
 */
@RestController
@RequestMapping("order")
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping
    @ApiOperation(value = "创建订单")
    public ResponseEntity<Long> createOrder(@RequestBody Order order){
        Long orderId = orderService.create(order);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 获取一条订单记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个订单数据")
    @SentinelResource("hot")
//    public ResponseEntity<OrderVo> getById(@PathVariable Integer id) {
//        OrderVo order = orderService.getById(id);
//        return ResponseEntity.status(HttpStatus.CREATED).body(order);
//    }
    public ApiResult getById(@PathVariable Integer id) {
        OrderVo order = orderService.getById(id);
        return ApiResult.ok(order);
        // return ApiResult.fail();
    }

    @GetMapping("/query")
    @ApiOperation(value ="查询订单")
    public String query() {
        orderService.queryGoods();
        System.out.println("查询订单");
        return "查询订单成功";
    }

    @GetMapping("/save")
    @ApiOperation(value ="新增订单")
    public String save() {
        orderService.queryGoods();
        System.out.println("新增订单");
        return "新增订单成功";
    }

    @GetMapping("/update")
    @ApiOperation(value = "更新订单")
    public String update() {
        return "更新订单成功";
    }


}
